﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Common;
using Nova_DAL;
using System.Drawing;
using Nova_Common;
using Nova_BLL;
using Nova_Common.Extend;
using System.Collections.Generic;
using Nova_MOD;

public partial class DelStorage_DelStorageList : System.Web.UI.Page
{
    DbHelper access = new DbHelper();
    TCommandBll CmdBll = new TCommandBll();
    TBase_StorageBll BaseStorageBll = new TBase_StorageBll();
    Nova_BLL.TBase_Position PositionBll = new Nova_BLL.TBase_Position();
    DbCommand cmd = null;
    DataSet ds = null;
    string strSQL = "";
    string strPID = "";
    int iback = 0;
    DataTable dtone = new DataTable();
    ArrayList ArrayPID = new ArrayList();
    ArrayList ArrayNum = new ArrayList();

    //获取出库单号所需的DB对象
    DbHelper access1 = new DbHelper();
    DbCommand cmd1 = null;
    DataSet ds1 = null;
    string strSQL1 = "";

    //用于双伸位前后货位的标记
    string FrontPos_ID = "";//用于标记上一行的货位号
    Color Samecolor = Color.AliceBlue;//两个切换的颜色
    Color Diffcolor = Color.LightSkyBlue;

    //20210426 NF项目 双伸位巷道集合 需要结合项目双伸位巷道修改
    string[] ExtensionLanelists = new string[] { "01" };
    string userName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.hidden.Value != "")
        {
            this.ViewState["PID"] = this.hidden.Value.ToString();
            this.hidden.Value = "";
            if (this.Session["dtone"] != null)
            {
                //下面的表只显示一个的出库单PID的出库记录，不要记忆上一次出库单PID的出库记录
                //this.dtone = (DataTable)this.Session["dtone"];
                this.DELSession();
                this.tempDtone();
            }
            else
            {
                this.tempDtone();
            }

            this.gvDatebind();
        }
        if (!base.IsPostBack)
        {
            //BindDllSQIDE();
            //this.ibtn_jianxian.Visible = true;
            //this.ibtn_InOut.Visible = true;
            //this.ibtnAllOUT.Visible = false;
            bind();
        }
        userName = Request.Cookies["vNovaUserNo"] == null ? "" : Request.Cookies["vNovaUserNo"].Value.ToString();
    }

    /// <summary>
    /// 根据库区号加载出口设备号
    /// </summary>
    //private void BindDllSQIDE()
    //{
    //    try
    //    {

    //        string sql = string.Format(@"select SQID from ECSStandard..TConveryerDefine where ifin=1 and LockState=0 ");

    //        this.cmd = this.access.GetSqlStringCommond(sql);
    //        ds = this.access.ExecuteDataSet(this.cmd);

    //        ddlSQIDE.DataTextField = "SQID";
    //        ddlSQIDE.DataValueField = "SQID";
    //        ddlSQIDE.DataSource = ds;
    //        ddlSQIDE.DataBind();
    //        ddlSQIDE.Items.Insert(0, new ListItem("自动分配出口号...", "0")); //0:自动分配出库号

    //    }
    //    catch (Exception ex)
    //    {

    //    }

    //}

    private void tempDtone()
    {
        this.dtone = new DataTable("dtone");
        this.dtone.Columns.Add("PID", Type.GetType("System.String"));//组盘主键的PID
        this.dtone.Columns.Add("Mat_Name", Type.GetType("System.String"));//物料名称
        this.dtone.Columns.Add("Mat_Code", Type.GetType("System.String"));//物料编码
        this.dtone.Columns.Add("Num", Type.GetType("System.Decimal"));
        this.dtone.Columns.Add("Mat_Num", Type.GetType("System.Decimal"));//库存
        //this.dtone.Columns.Add("TS01", Type.GetType("System.String"));
        //this.dtone.Columns.Add("TM02", Type.GetType("System.String"));
        //this.dtone.Columns.Add("TM03", Type.GetType("System.String"));
        //this.dtone.Columns.Add("S01", Type.GetType("System.String"));
        //this.dtone.Columns.Add("S02", Type.GetType("System.String"));
        //this.dtone.Columns.Add("S03", Type.GetType("System.String"));
        //this.dtone.Columns.Add("S04", Type.GetType("System.String"));
        //this.dtone.Columns.Add("S06", Type.GetType("System.String"));
        //this.dtone.Columns.Add("S10", Type.GetType("System.String"));
        this.dtone.Columns.Add("delPID", Type.GetType("System.String"));// 出库单PID
        this.dtone.Columns.Add("StockNo", Type.GetType("System.String"));// 出库单号
        this.dtone.Columns.Add("Tray_id", Type.GetType("System.String"));// Tray_id托盘号
        this.dtone.Columns.Add("Pos_ID", Type.GetType("System.String"));// 货位号
        //this.dtone.Columns.Add("Lane_ID", Type.GetType("System.String"));// 道口号
        //this.dtone.Columns.Add("TS02", Type.GetType("System.String"));
        this.Session["dtone"] = this.dtone;
    }

    protected void bind()
    {
        this.labMsg.Text = "";
        this.strSQL = @"Select DISTINCT TBM.Mat_Name,TBM.Mat_Code,TBM.Unit_Base,TBM.S01,TBM.TM01,TBM.TM03,TBM.S02,TDS.StockNo,TDS.Num,TDS.Act_Num,
                                    TDS.PID,TDS.TS01,TDS.S05,TDS.CDT,ISNULL(TCD.DETAIL_PID,0) as DETAIL_PID
                                    FROM TDelStorage_List TDS LEFT JOIN dbo.TBill_DelStorage TBD ON TDS.StockNo = TBD.StockNo
                                    LEFT JOIN TBase_Material TBM ON TDS.Mat_Code = TBM.Mat_Code
                                    LEFT JOIN TCommandsDetail TCD ON TCD.DETAIL_PID=TDS.PID
                                    WHERE 1=1 AND TBD.Small_BillType = 11";

        if (this.txtMat_Code.Text != "")
        {
            this.strSQL = this.strSQL + " And TDS.Mat_Code like '%" + this.txtMat_Code.Text.Trim() + "%'";
        }
        if (this.txtStorageNo.Text != "")
        {
            this.strSQL = this.strSQL + " And TDS.StockNo like '%" + this.txtStorageNo.Text.Trim() + "%'";
        }
        if (!string.IsNullOrEmpty(txtMat_Name.Text))
        {
            this.strSQL = this.strSQL + " And TBM.Mat_Name like '%" + this.txtMat_Name.Text.Trim() + "%'";
        }
        if (txt_TS01.Text.Trim() != "")
        {
            this.strSQL = this.strSQL + " And TDS.TS01 like '%" + this.txt_TS01.Text.Trim() + "%'";
        }
        if (this.txtDateS.Text != "")
        {
            this.strSQL = this.strSQL + " And TDS.CDT>=convert(datetime,'" + this.txtDateS.Text.Trim() + "')";
        }
        if (this.txtDateE.Text != "")
        {
            this.strSQL = this.strSQL + " And TDS.CDT<=dateadd(d,1,convert(datetime,'" + this.txtDateE.Text.Trim() + "'))";
        }
        strSQL += " ORDER BY  TDS.StockNo desc";
        this.cmd = this.access.GetSqlStringCommond(this.strSQL);
        this.gv.DataSource = this.access.ExecuteDataSet(this.cmd);
        this.gv.DataBind();
        if (this.gv.Rows.Count <= 0)
        {
            this.labMsg.Text = "不存在相应的单据";
        }
    }



    protected void DELSession()
    {
        this.labMsg.Text = "";
        this.Session["dtone"] = null;
        this.gvDate.DataSource = null;
        this.gvDate.DataBind();
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.gv.EditIndex = -1;
        this.bind();
    }


    /// <summary>
    /// 订单明细中有关联正在执行的指令时，显示红色 自动出库操作按钮隐藏
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            LinkButton button = (LinkButton)e.Row.FindControl("lbtn_Mat_Name");
            Label labCount = (Label)e.Row.FindControl("lab_BillMatCount");
            Label labTS01 = (Label)e.Row.FindControl("lab_TS01");
            Label labNum = (Label)e.Row.FindControl("labNum");
            Label labActNum = (Label)e.Row.FindControl("labAct_Num");
            //string str = string.Format("javascript: document.getElementById('{1}').value=window.showModalDialog('SearchStock.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") +
            //    "&strMatCode={0}&delPID={2}&billMatCount={3}&TS01={4}', window, 'dialogWidth=900px;dialogHeight=530px;scroll:no;resizable:no;status:0;');document.URL=location.href;",
            //    button.CommandArgument.ToString(), this.hidden.ClientID, this.gv.DataKeys[e.Row.RowIndex].Value.ToString(), labCount.Text, labTS01.Text);

            string str = string.Format("javascript:document.getElementById('{1}').value=window.open('SearchStock.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&strMatCode={0}&delPID={2}&billMatCount={3}&TS01={4}', window, 'Height=530px,Width=1160px,top=200, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;void(0);",
                 button.CommandArgument.ToString(), this.hidden.ClientID, this.gv.DataKeys[e.Row.RowIndex].Value.ToString(), labCount.Text, labTS01.Text);
            button.Attributes.Add("onclick", str);

            Label lab_DETAIL_PID = (Label)e.Row.FindControl("lab_DETAIL_PID");
            ImageButton ibtnAutoOut = (ImageButton)e.Row.FindControl("ibtnAutoOut");
            if (lab_DETAIL_PID.Text != "0")  //Command_detail表中存在显示红色，表示正在处理
            {
                e.Row.ForeColor = Color.FromName("red");
                ibtnAutoOut.Visible = false;   //订单明细正在处理的，自动操作按钮隐藏
            }
            if (decimal.Parse(labNum.Text) <= decimal.Parse(labActNum.Text))
            {
                ibtnAutoOut.Visible = false;
            }
        }
    }

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.gv.EditIndex = e.NewEditIndex;
        this.bind();
    }

    //protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    //{
    //    string str = this.gv.DataKeys[e.RowIndex].Value.ToString();
    //    TextBox box = (TextBox)this.gv.Rows[e.RowIndex].FindControl("txtTS01");
    //    this.strSQL = "update TDelStorage_List set TS01='" + box.Text.Trim() + "' where PID='" + str + "'";
    //    try
    //    {
    //        this.cmd = this.access.GetSqlStringCommond(this.strSQL);
    //        this.access.ExecuteNonQuery(this.cmd);
    //        this.gv.EditIndex = -1;
    //        this.bind();
    //    }
    //    catch (Exception exception)
    //    {
    //        base.Response.Write(exception.ToString());
    //    }
    //}
    protected string GetStockNo(long delPID)
    {
        string StockNo = "";
        try
        {

            this.strSQL1 = "Select StockNo From TDelStorage_List where PID=" + delPID + "";
            this.cmd1 = this.access.GetSqlStringCommond(this.strSQL1);
            this.ds1 = this.access.ExecuteDataSet(this.cmd1);
            if (this.ds1.Tables[0].Rows.Count > 0)
            {
                StockNo = this.ds1.Tables[0].Rows[0]["StockNo"].ToString();
            }
            return StockNo;
        }


        catch (Exception)
        {

            return StockNo;

        }
    }

    protected void gvDatebind()
    {
        if ((this.ViewState["PID"] != null) && (this.ViewState["PID"].ToString() != ""))
        {
            this.ArrayPID = new ArrayList(this.ViewState["PID"].ToString().Split(new char[] { ',' }));
            for (int i = 0; i < this.ArrayPID.Count; i++)
            {
                this.ArrayNum = new ArrayList(this.ArrayPID[i].ToString().Split(new char[] { '|' }));
                this.strSQL = @"Select TBM.Mat_Name,TBM.Mat_Code,TBS.PID,TBS.Mat_Num 
                         From TBase_Storage TBS LEFT JOIN TBase_InterFace TBI ON TBS.Sec_Mat_ID = TBI.Sec_Mat_ID 
                         LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TBI.Mat_Code
                         Where TBS.PID = '" + this.ArrayNum[0].ToString() + "'  ";
                this.cmd = this.access.GetSqlStringCommond(this.strSQL);
                this.ds = this.access.ExecuteDataSet(this.cmd);
                if (this.ds.Tables[0].Rows.Count > 0)
                {
                    string StockNo = GetStockNo(Convert.ToInt64(this.ArrayNum[2].ToString()));//通过出库单PDI获取出库单号

                    if (this.dtone.Rows.Count > 0)
                    {
                        for (int j = 0; j < this.dtone.Rows.Count; j++)
                        {
                            this.strPID = this.strPID + this.dtone.Rows[j]["PID"].ToString() + ",";
                        }
                        this.iback = this.strPID.LastIndexOf(this.ArrayNum[0].ToString());
                        if (this.iback < 0)
                        {
                            DataRow row = this.dtone.NewRow();
                            row["PID"] = this.ds.Tables[0].Rows[0]["PID"].ToString();
                            row["Mat_Name"] = this.ds.Tables[0].Rows[0]["Mat_Name"].ToString();
                            row["Mat_Code"] = this.ds.Tables[0].Rows[0]["Mat_Code"].ToString();
                            row["Num"] = this.ArrayNum[1].ToString();//应发数量（默认为0，此值没有使用）  //this.ArrayNum[6].ToString();
                            row["Mat_Num"] = this.ArrayNum[3].ToString();//库存数量
                            //row["TS01"] = this.ds.Tables[0].Rows[0]["TS01"].ToString();
                            //row["TM02"] = this.ds.Tables[0].Rows[0]["TM02"].ToString();
                            //row["TM03"] = this.ds.Tables[0].Rows[0]["TM03"].ToString();
                            //row["S01"] = this.ds.Tables[0].Rows[0]["S01"].ToString();
                            //row["S02"] = this.ds.Tables[0].Rows[0]["S02"].ToString();
                            //row["S03"] = this.ds.Tables[0].Rows[0]["S03"].ToString();
                            //row["S04"] = this.ds.Tables[0].Rows[0]["S04"].ToString();
                            //row["S06"] = this.ds.Tables[0].Rows[0]["S06"].ToString();
                            //row["S10"] = this.ds.Tables[0].Rows[0]["S10"].ToString();
                            row["delPID"] = this.ArrayNum[2].ToString();
                            row["StockNo"] = StockNo;
                            row["Tray_id"] = this.ArrayNum[4].ToString();
                            row["Pos_ID"] = this.ArrayNum[5].ToString();
                            //row["Lane_ID"] = this.ArrayNum[6].ToString();
                            //row["TS02"] = this.ds.Tables[0].Rows[0]["TS02"].ToString();
                            this.dtone.Rows.Add(row);
                        }
                        else
                        {
                            for (int k = 0; k < this.dtone.Rows.Count; k++)
                            {
                                if ((this.dtone.Rows[k]["PID"].ToString() == this.ArrayNum[0].ToString()) && (Convert.ToInt32(this.dtone.Rows[k]["Num"].ToString()) < Convert.ToInt32(this.ArrayNum[1].ToString())))
                                {
                                    this.dtone.Rows.RemoveAt(k);
                                    DataRow row2 = this.dtone.NewRow();
                                    row2["PID"] = this.ds.Tables[0].Rows[0]["PID"].ToString();
                                    row2["Mat_Name"] = this.ds.Tables[0].Rows[0]["Mat_Name"].ToString();
                                    row2["Mat_Code"] = this.ds.Tables[0].Rows[0]["Mat_Code"].ToString();
                                    row2["Num"] = this.ArrayNum[1].ToString();//this.ArrayNum[6].ToString();
                                    row2["Mat_Num"] = this.ArrayNum[3].ToString();
                                    //row2["TS01"] = this.ds.Tables[0].Rows[0]["TS01"].ToString();
                                    //row2["TM02"] = this.ds.Tables[0].Rows[0]["TM02"].ToString();
                                    //row2["TM03"] = this.ds.Tables[0].Rows[0]["TM03"].ToString();
                                    //row2["S01"] = this.ds.Tables[0].Rows[0]["S01"].ToString();
                                    //row2["S02"] = this.ds.Tables[0].Rows[0]["S02"].ToString();
                                    //row2["S03"] = this.ds.Tables[0].Rows[0]["S03"].ToString();
                                    //row2["S04"] = this.ds.Tables[0].Rows[0]["S04"].ToString();
                                    //row2["S06"] = this.ds.Tables[0].Rows[0]["S06"].ToString();
                                    //row2["S10"] = this.ds.Tables[0].Rows[0]["S10"].ToString();
                                    row2["delPID"] = this.ArrayNum[2].ToString();
                                    row2["StockNo"] = StockNo;
                                    row2["Tray_id"] = this.ArrayNum[4].ToString();
                                    row2["Pos_ID"] = this.ArrayNum[5].ToString();
                                    //row2["Lane_ID"] = this.ArrayNum[6].ToString();
                                    //row2["TS02"] = this.ds.Tables[0].Rows[0]["TS02"].ToString();
                                    this.dtone.Rows.Add(row2);
                                }
                            }
                        }
                    }
                    else
                    {
                        DataRow row3 = this.dtone.NewRow();
                        row3["PID"] = this.ds.Tables[0].Rows[0]["PID"].ToString();
                        row3["Mat_Name"] = this.ds.Tables[0].Rows[0]["Mat_Name"].ToString();
                        row3["Mat_Code"] = this.ds.Tables[0].Rows[0]["Mat_Code"].ToString();
                        row3["Num"] = this.ArrayNum[1].ToString();//this.ArrayNum[6].ToString();
                        row3["Mat_Num"] = this.ArrayNum[3].ToString();
                        //row3["TS01"] = this.ds.Tables[0].Rows[0]["TS01"].ToString();
                        //row3["TM02"] = this.ds.Tables[0].Rows[0]["TM02"].ToString();
                        //row3["TM03"] = this.ds.Tables[0].Rows[0]["TM03"].ToString();
                        //row3["S01"] = this.ds.Tables[0].Rows[0]["S01"].ToString();
                        //row3["S02"] = this.ds.Tables[0].Rows[0]["S02"].ToString();
                        //row3["S03"] = this.ds.Tables[0].Rows[0]["S03"].ToString();
                        //row3["S04"] = this.ds.Tables[0].Rows[0]["S04"].ToString();
                        //row3["S06"] = this.ds.Tables[0].Rows[0]["S06"].ToString();
                        //row3["S10"] = this.ds.Tables[0].Rows[0]["S10"].ToString();
                        row3["delPID"] = this.ArrayNum[2].ToString();
                        row3["StockNo"] = StockNo;
                        row3["Tray_id"] = this.ArrayNum[4].ToString();
                        row3["Pos_ID"] = this.ArrayNum[5].ToString();
                        //row3["Lane_ID"] = this.ArrayNum[6].ToString();
                        //row3["TS02"] = this.ds.Tables[0].Rows[0]["TS02"].ToString();
                        this.dtone.Rows.Add(row3);
                    }
                }
            }
        }
        this.gvDate.DataSource = this.Session["dtone"];
        this.gvDate.DataBind();
    }

    protected void ibt_reset_Click(object sender, ImageClickEventArgs e)
    {
        this.DELSession();
        this.Label1.Text = "";
    }

    protected void btn_Reset1_Click(object sender, ImageClickEventArgs e)
    {

        this.Label1.Text = "";
        this.txtStorageNo.Text = "";
        this.txtMat_Code.Text = "";
        this.txtDateS.Text = "";
        this.txtDateE.Text = "";
        this.ddlSQIDE.SelectedIndex = 0;
    }


    /// <summary>
    /// xgw 20210426 add NF项目 拣选/整出(离线)  自动根据出库的数量 判定是整出/拣选
    /// 双伸位巷道后面的货位（偶数层）添加能否出库的业务逻辑判断，不满足条件提示，跳出； 
    /// 判断前面货位的状态，如果是入库约定、移库约定 则提示跳出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtn_InOut_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (gvDate.Rows.Count <= 0)
                return;
            #region 出库数量的核对
            this.strSQL = "SELECT * FROM TDelStorage_List WHERE PID = '" + this.gvDate.Rows[0].Cells[4].Text.ToString() + "'";//出库单的 PID
            this.cmd = this.access.GetSqlStringCommond(this.strSQL);
            this.ds = this.access.ExecuteDataSet(this.cmd);

            Decimal MaxDelNum = 0;//最大出库量
            Decimal Num = 0;// 选择的出库数量
            if (this.ds.Tables[0].Rows.Count > 0)
            {
                MaxDelNum = Decimal.Parse(this.ds.Tables[0].Rows[0]["Num"].ToString()) - Decimal.Parse(this.ds.Tables[0].Rows[0]["Act_Num"].ToString());

            }
            if (MaxDelNum <= 0)
            {
                this.Label1.Text = "此出库单明细中货物实际出库量不能大于计划出库量，不能对此出库明细做出库操作！";
                return;
            }


            TextBox box = (TextBox)this.gvDate.Rows[0].FindControl("txtNum");
            if (Decimal.Parse(box.Text) == 0)
            {
                this.Label1.Text = "填写的出库数量为零，请重新填写数量！";
                return;
            }
            string labStockNum = gvDate.Rows[0].Cells[3].Text.ToString();
            if (decimal.Parse(box.Text) > decimal.Parse(labStockNum))
            {
                this.Label1.Text = "填写的出库数量不能大于库存数量，请重新填写数量！";
                return;
            }

            Num = Num + Decimal.Parse(box.Text);

            if (Num > MaxDelNum || Num == 0)
            {
                this.Label1.Text = "填写的出库数量有误，请重新填写数量！";
                return;
            }
            #endregion

            #region xgw 20200702 add 1、 双伸位巷道（2#巷道） 后面的货位（偶数层）添加能否出库的业务逻辑判断，不满足条件提示，跳出；   
            //for (int k = 0; k < this.gvDate.Rows.Count; k++)
            //{
            //    string Pos_Id = this.gvDate.Rows[k].Cells[7].Text.ToString();
            //    bool SeleFront = true; //是否选择了前面货位
            //    if ((Convert.ToInt32(Pos_Id.Substring(8, 2)) % 2) == 0 && Pos_Id.Substring(2, 2) == "02")// 双伸位 后面的货位
            //    {
            //        if (k == 0)
            //        {
            //            SeleFront = false;

            //        }

            //        if (k > 0)
            //        {
            //            if (((Convert.ToInt64(Pos_Id.Substring(0, 10)) - 1) * 100) != Convert.ToInt64(this.gvDate.Rows[k - 1].Cells[7].Text.ToString()))
            //            {
            //                SeleFront = false;
            //            }
            //        }

            //        if (!SeleFront)
            //        {
            //            long Pos_ID_Front = (Convert.ToInt64(Pos_Id.Substring(0, 10)) - 1) * 100;
            //            string str1 = string.Format(@"select * from  dbo.TBase_Position where Pos_ID={0}
            //        ", Pos_ID_Front);

            //            cmd = access.GetSqlStringCommond(str1);
            //            ds = access.ExecuteDataSet(cmd);
            //            if (ds.Tables[0].Rows.Count > 0)
            //            {
            //                if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 2)
            //                {
            //                    this.Label1.Text = Pos_Id + "前面的货位有入库预定，此货位的货物不能出库！";
            //                    return;
            //                }

            //                if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 1)
            //                {
            //                    this.Label1.Text = Pos_Id + "前面的货位有货，" + Pos_ID_Front.ToString() + "货位的货必须选择出库， 此货位的货物才能出库！";
            //                    return;
            //                }

            //                if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 6)
            //                {
            //                    this.Label1.Text = Pos_Id + "前面的货位有移库预定，此货位的货物不能出库！";
            //                    return;
            //                }

            //            }
            //        }

            //    }

            //}
            #endregion

            #region xgw 20210426 add 1、 双伸位巷道 后面的货位（偶数层）添加能否出库的业务逻辑判断，不满足条件提示，跳出；   
            //for (int k = 0; k < this.gvDate.Rows.Count; k++)
            //{
            //    string Pos_Id = this.gvDate.Rows[k].Cells[7].Text.ToString();

            //    if ((Convert.ToInt32(Pos_Id.Substring(8, 2)) % 2) == 0 && ExtensionLanelists.Contains(Pos_Id.Substring(2, 2)))// 双伸位 后面的货位
            //    {

            //        long Pos_ID_Front = (Convert.ToInt64(Pos_Id.Substring(0, 10)) - 1) * 100;
            //        string str1 = string.Format(@"select * from  TBase_Position where Pos_ID={0}", Pos_ID_Front);

            //        cmd = access.GetSqlStringCommond(str1);
            //        ds = access.ExecuteDataSet(cmd);
            //        if (ds.Tables[0].Rows.Count > 0)
            //        {
            //            if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 2)
            //            {
            //                this.Label1.Text = Pos_Id + "前面的货位有入库预定，此货位的货物不能出库！";
            //                return;
            //            }


            //            if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 6)
            //            {
            //                this.Label1.Text = Pos_Id + "前面的货位有移库预定，此货位的货物不能出库！";
            //                return;
            //            }

            //        }


            //    }

            //}
            #endregion

            // 获取出库口设备编号(出库只能往1004口出)
            int sqid = 1004;
            if (!CmdBll.IsDeviceEnable(sqid))
            {
                Label1.Text = "出库路径输送线设备、巷道处于禁用或者离线模式!";
                //ShowBox.MessageBox(this.Page, "出库口设备现在处于禁用或者离线模式,无法下发指令!");
                return;
            }

            this.cmd = this.access.GetStoredProcCommond("SP_START_OUTPUT"); // 执行DELETE FROM TmpGenerateOutCMDs_Define WHERE CID=HOST_NAME()
            this.access.ExecuteNonQuery(this.cmd);

            for (int j = 0; j < this.gvDate.Rows.Count; j++)  //插入临表TmpGenerateOutCMDs_Define
            {
                this.cmd = this.access.GetStoredProcCommond("SP_INSERT_FOR_OUT");
                this.access.AddInParameter(this.cmd, "@STORE_PID", DbType.String, this.gvDate.DataKeys[j].Value.ToString());
                TextBox box2 = (TextBox)this.gvDate.Rows[j].FindControl("txtNum");
                this.access.AddInParameter(this.cmd, "@MAT_NUM", DbType.String, box2.Text.ToString());
                this.access.AddInParameter(this.cmd, "@DETAIL_PID", DbType.Decimal, this.gvDate.Rows[j].Cells[4].Text.ToString());
                this.access.ExecuteNonQuery(this.cmd);
            }

            int isEmptyTray = 0;
            string trayId = gvDate.Rows[0].Cells[6].Text.ToString();
            int matTypeCount = BaseStorageBll.GetMetarialTypeCount(trayId);
            if (matTypeCount == 1)
            {
                isEmptyTray = (decimal.Parse(box.Text) >= decimal.Parse(labStockNum)) ? 1 : 0;
            }

            //这里是本身的方法
            this.cmd = this.access.GetStoredProcCommond("SP_GenerateOUTCMDS_Define");
            this.access.AddInParameter(this.cmd, "@EquipmentE", DbType.Int32, sqid);//终点设备，0:表示自动选择 其他：人工指定出口
            this.access.AddInParameter(this.cmd, "@MEMO", DbType.String, "");
            this.access.AddInParameter(this.cmd, "@FORCE_PICK", DbType.Int16, 0);  //>0 
            this.access.AddInParameter(this.cmd, "@USER_NAME", DbType.String, base.Request.Cookies["vNovaUserNo"].Value.ToString());
            this.access.AddInParameter(this.cmd, "@IsEmptyTray", DbType.Int32, isEmptyTray);
            this.access.AddOutParameter(this.cmd, "@RESNO", DbType.Int32, 100);
            this.access.AddOutParameter(this.cmd, "@ErrMsg", DbType.String, 0xff);
            this.access.ExecuteNonQuery(this.cmd);
            int num3 = Convert.ToInt32(this.access.GetParameter(this.cmd, "@RESNO").Value.ToString());
            string str = this.access.GetParameter(this.cmd, "@ErrMsg").Value.ToString();
            if (num3 > 0)
            {
                this.Label1.Text = "操作成功！";

            }
            else
            {
                this.Label1.Text = str.ToString();
            }

            bind();
            this.DELSession();
        }
        catch (Exception ex)
        {
            this.Label1.Text = "程序执行异常跳出！";
        }

    }



    /// <summary>
    /// 编辑的删除操作
    ///20200702  增加双伸位 外侧货物撤销的核对（如果内侧货物在，则此外侧货物不允许撤销）
    ///20200426  删除  双伸位 外侧货物撤销的核对  撤销不需要核对
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtndelete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton button = (ImageButton)sender;
        int index = Convert.ToInt32(button.CommandArgument.ToString());

        #region  能否删除的核对
        //string Pos_Id = this.gvDate.Rows[index].Cells[7].Text.ToString(); 
        //if ((Convert.ToInt32(Pos_Id.Substring(8, 2)) % 2) == 1 && Pos_Id.Substring(2, 2) == "02")// 双伸位 前面的货位
        //{
        //    if (index < this.gvDate.Rows.Count - 1)  //最后一行 不用比对
        //    {
        //        if (((Convert.ToInt64(Pos_Id.Substring(0, 10)) + 1) * 100) == Convert.ToInt64(this.gvDate.Rows[index + 1].Cells[7].Text.ToString()))
        //        {
        //            this.Label1.Text = Pos_Id + string.Format("：后面的货{0}在待出库列表中，此货位的货物不能删除！", ((Convert.ToInt64(Pos_Id.Substring(0, 10)) + 1) * 100).ToString());
        //            return;
        //        }
        //    }
        //}

        #endregion

        if (this.Session["dtone"] != null)
        {
            this.dtone = (DataTable)this.Session["dtone"];
            this.dtone.Rows.RemoveAt(index);
            this.gvDate.DataSource = this.dtone;
            this.gvDate.DataBind();
        }

    }
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.bind();
    }



    //在线拣选 按钮隐藏
    protected void ibtn_jianxian_Click(object sender, ImageClickEventArgs e)
    {
        this.cmd = this.access.GetStoredProcCommond("SP_START_OUTPUT");
        this.access.ExecuteNonQuery(this.cmd);
        for (int i = 0; i < this.gvDate.Rows.Count; i++)
        {
            TextBox box = (TextBox)this.gvDate.Rows[i].FindControl("txtNum");
            //Label label = (Label)this.gvDate.Rows[i].FindControl("lblTS02");//评估类型
            //Label label2 = (Label)this.gvDate.Rows[i].FindControl("lblS03");//批号
            //根据出库单明细PID获取
            this.strSQL = "SELECT * FROM TDelStorage_List WHERE PID = '" + this.gvDate.Rows[i].Cells[4].Text.ToString() + "'";
            this.cmd = this.access.GetSqlStringCommond(this.strSQL);
            this.ds = this.access.ExecuteDataSet(this.cmd);
            //缺少出库数量选择的判断
            if (this.ds.Tables[0].Rows.Count > 0)
            {
                if (decimal.Parse(this.ds.Tables[0].Rows[0]["Num"].ToString()) < (decimal.Parse(this.ds.Tables[0].Rows[0]["Act_Num"].ToString()) + decimal.Parse(box.Text.ToString())))
                {
                    this.Label1.Text = "重新填写数量，出库数量不能大于计划数量！";
                    return;
                }

            }
        }
        for (int j = 0; j < this.gvDate.Rows.Count; j++)
        {
            this.cmd = this.access.GetStoredProcCommond("SP_INSERT_FOR_OUT");
            this.access.AddInParameter(this.cmd, "@STORE_PID", DbType.String, this.gvDate.DataKeys[j].Value.ToString());//库存明细PID
            TextBox box2 = (TextBox)this.gvDate.Rows[j].FindControl("txtNum");
            this.access.AddInParameter(this.cmd, "@MAT_NUM", DbType.String, box2.Text.ToString());//实际出库数
            this.access.AddInParameter(this.cmd, "@DETAIL_PID", DbType.Decimal, this.gvDate.Rows[j].Cells[4].Text.ToString());//出库单明细PID
            this.access.ExecuteNonQuery(this.cmd);
        }
        this.cmd = this.access.GetStoredProcCommond("SP_GenerateOUTCMDS_Define");
        this.access.AddInParameter(this.cmd, "@EquipmentE", DbType.Int16, 0);//终点设备，此值无实际意义，接口需要
        this.access.AddInParameter(this.cmd, "@MEMO", DbType.String, "");
        this.access.AddInParameter(this.cmd, "@FORCE_PICK", DbType.Int16, 1);
        this.access.AddInParameter(this.cmd, "@USER_NAME", DbType.String, base.Request.Cookies["vNovaUserNo"].Value.ToString());
        this.access.AddOutParameter(this.cmd, "@RESNO", DbType.Int32, 100);
        this.access.AddOutParameter(this.cmd, "@ErrMsg", DbType.String, 0xff);
        this.access.ExecuteNonQuery(this.cmd);
        int num3 = Convert.ToInt32(this.access.GetParameter(this.cmd, "@RESNO").Value.ToString());
        string str = this.access.GetParameter(this.cmd, "@ErrMsg").Value.ToString();
        if (num3 > 0)
        {
            this.Label1.Text = "操作成功！";
            bind();
        }
        else
        {
            this.Label1.Text = str.ToString();
        }

        this.DELSession();
    }

    /// <summary>
    /// 离线整出  按钮隐藏
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnAllOUT_Click(object sender, ImageClickEventArgs e)
    {
        this.labMsg.Text = "";
        this.cmd = this.access.GetStoredProcCommond("SP_START_OUTPUT");
        this.access.ExecuteNonQuery(this.cmd);
        //出库数量的验证（只对一行数据的验证，没有考虑全面？）
        for (int i = 0; i < this.gvDate.Rows.Count; i++)
        {
            TextBox box = (TextBox)this.gvDate.Rows[i].FindControl("txtNum");
            box.Text = this.gvDate.Rows[i].Cells[3].Text.ToString();//整出等于库存的数量
            this.strSQL = "SELECT * FROM TDelStorage_List WHERE PID = '" + this.gvDate.Rows[i].Cells[4].Text.ToString() + "'";
            this.cmd = this.access.GetSqlStringCommond(this.strSQL);
            this.ds = this.access.ExecuteDataSet(this.cmd);
            if (this.ds.Tables[0].Rows.Count > 0)
            {
                if (decimal.Parse(this.ds.Tables[0].Rows[0]["Num"].ToString()) < (decimal.Parse(this.ds.Tables[0].Rows[0]["Act_Num"].ToString()) + decimal.Parse(box.Text.ToString())))
                {
                    this.Label1.Text = "重新填写数量，出库数量不能大于计划数量！";
                    return;
                }

            }
        }


        for (int j = 0; j < this.gvDate.Rows.Count; j++)
        {
            this.cmd = this.access.GetStoredProcCommond("SP_INSERT_FOR_OUT");
            this.access.AddInParameter(this.cmd, "@STORE_PID", DbType.String, this.gvDate.DataKeys[j].Value.ToString());
            TextBox box2 = (TextBox)this.gvDate.Rows[j].FindControl("txtNum");
            this.access.AddInParameter(this.cmd, "@MAT_NUM", DbType.String, box2.Text.ToString());
            this.access.AddInParameter(this.cmd, "@DETAIL_PID", DbType.Decimal, this.gvDate.Rows[j].Cells[4].Text.ToString());
            this.access.ExecuteNonQuery(this.cmd);
        }
        this.cmd = this.access.GetStoredProcCommond("SP_GenerateOUTCMDS_Define");
        this.access.AddInParameter(this.cmd, "@EquipmentE", DbType.Int16, 0);//终点设备，0代表需要从函数中获取；非0代表输入的终端设备号
        this.access.AddInParameter(this.cmd, "@MEMO", DbType.String, "");
        this.access.AddInParameter(this.cmd, "@FORCE_PICK", DbType.Int16, 2);
        this.access.AddInParameter(this.cmd, "@USER_NAME", DbType.String, base.Request.Cookies["vNovaUserNo"].Value.ToString());
        this.access.AddOutParameter(this.cmd, "@RESNO", DbType.Int32, 100);
        this.access.AddOutParameter(this.cmd, "@ErrMsg", DbType.String, 0xff);
        this.access.ExecuteNonQuery(this.cmd);
        int num3 = Convert.ToInt32(this.access.GetParameter(this.cmd, "@RESNO").Value.ToString());
        string str = this.access.GetParameter(this.cmd, "@ErrMsg").Value.ToString();
        if (num3 > 0)
        {
            this.Label1.Text = "操作成功！";
            bind();
        }
        else
        {
            this.Label1.Text = str.ToString();
        }
        this.DELSession();
    }


    /// <summary>
    /// 双伸位前后货位关系 的显示同意颜色区分
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string Pos_Id = e.Row.Cells[7].Text.ToString();

                //第二巷道（双伸位） 后面货位（偶数层） 与前一行数据货位为前后关系  显示同样颜色， 否则显示不同颜色
                if ((Convert.ToInt32(Pos_Id.Substring(8, 2)) % 2) == 0 && ExtensionLanelists.Contains(Pos_Id.Substring(2, 2)) && ((Convert.ToInt64(Pos_Id.Substring(0, 10)) - 1) * 100)._ToString() == FrontPos_ID)
                {
                    e.Row.BackColor = Samecolor;

                }
                else
                {
                    e.Row.BackColor = Diffcolor;

                    Color Middlecolor = Samecolor;
                    Samecolor = Diffcolor;
                    Diffcolor = Middlecolor;

                }

                FrontPos_ID = Pos_Id;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());


        }
    }



    /// <summary>
    /// 自动出库操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnAutoOut_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            ImageButton ibtnAutoOut = (ImageButton)sender;
            if (!string.IsNullOrEmpty(ibtnAutoOut.CommandArgument.Trim()))
            {
                long DETAIL_PID = long.Parse(ibtnAutoOut.CommandArgument);

                // 获取出库口设备编号(出库只能往1004口出)
                int sqid = 1004;
                if (!CmdBll.IsDeviceEnable(sqid))
                {
                    Label1.Text = "出库路径输送线设备、巷道处于禁用或者离线模式!";
                    //ShowBox.MessageBox(this.Page, "出库口设备现在处于禁用或者离线模式,无法下发指令!");
                    return;
                }

                this.cmd = this.access.GetStoredProcCommond("SP_Auto_GenerateOUTCMDS_BY_DelStorageDetail_YMJX");
                this.access.AddInParameter(this.cmd, "@DETAIL_PID", DbType.Int64, DETAIL_PID);//  出库单明细主键 PID
                this.access.AddInParameter(this.cmd, "@EquipmentE", DbType.Int32, sqid);//终点设备，0:表示自动选择 其他：人工指定出口
                this.access.AddInParameter(this.cmd, "@MEMO", DbType.String, "");
                this.access.AddInParameter(this.cmd, "@FORCE_PICK", DbType.Int16, 0);  //1：强制在线 （拣选、整出） 指令  0：不强制
                this.access.AddInParameter(this.cmd, "@USER_NAME", DbType.String, base.Request.Cookies["vNovaUserNo"].Value.ToString());
                this.access.AddOutParameter(this.cmd, "@ResNO", DbType.Int32, 100);
                this.access.AddOutParameter(this.cmd, "@ResMsg", DbType.String, 0xff);
                this.access.ExecuteNonQuery(this.cmd);
                int res = Convert.ToInt32(this.access.GetParameter(this.cmd, "@ResNO").Value.ToString());
                string str = this.access.GetParameter(this.cmd, "@ResMsg").Value.ToString();
                if (res > 0)
                {
                    this.Label1.Text = "操作成功！" + str;

                }
                else
                {
                    this.Label1.Text = "操作出现错误！" + str;
                }
            }

            this.bind(); ;  //刷新加载  

        }
        catch (Exception ex)
        {
            this.Label1.Text = "程序执行异常跳出！";
        }

    }
}